package com.example.gymmanagement.controller;

import com.example.gymmanagement.Utils.StatusCode;
import com.example.gymmanagement.domain.User;
import com.example.gymmanagement.service.UserService;
import io.swagger.annotations.ApiOperation;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.gymmanagement.Utils.Result;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/user")

public class UserController {
    @Autowired
    private UserService userService;

    //新增用户,参数少个token，@RequestHeader("Authorization") String token
    @PostMapping
    @ApiOperation("添加用户")
    public Result save(@RequestBody User user,@RequestHeader("Authorization") String token){
        User user1 = userService.selectById(user.getUserNum().intValue());
        if (user1!=null){
            return new Result(StatusCode.SAVE_ERR,false,"用户已存在");
        }
        user.setUserCreatetime(LocalDateTime.now());
        user.setUserCred(100);
        boolean flag = this.userService.insert(user);
        String msg = flag?"添加用户成功":"添加用户失败";
        return new Result(flag?StatusCode.SAVE_OK:StatusCode.SAVE_ERR,flag,msg);
    }

    //查询用户，按id（学生学号）,参数还需要token
    @GetMapping("/{id}")
    @ApiOperation("按id查询用户")
    public Result selectById(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        User user = this.userService.selectById(id);
        Integer code = user!=null?StatusCode.GET_OK:StatusCode.GET_ERR;
        String msg = user!=null?"查询成功":"查询数据失败！";
        return new Result(code,user,msg);
    }

    //删除用户，按id删除，缺参数token
    @PostMapping("/delete/{id}")
    @ApiOperation("按id删除用户")
    public Result deleteBuId(@PathVariable Integer id,@RequestHeader("Authorization") String token){
        boolean flag = this.userService.deleteById(id);
        String msg = flag?"删除成功":"删除失败";
        return new Result(flag?StatusCode.DELETE_OK:StatusCode.DELETE_ERR,flag,msg);
    }
    //更新用户信息，缺参数token
    @PostMapping("/update")
    @ApiOperation("更新用户")
    public Result updateUser(@RequestBody User user,@RequestHeader("Authorization") String token){
        user.setUserUpdatetime(LocalDateTime.now());
        boolean flag = this.userService.updateUser(user);
        String msg=flag?"修改成功":"修改失败";
        return new Result(flag?StatusCode.UPDATE_OK:StatusCode.UPDATE_ERR,flag,msg);
    }

    //查询所有用户
    @GetMapping
    @ApiOperation("查找所有用户（分页）")
    public Result selectList(@RequestHeader("Authorization") String token,Integer index,Integer size){
        Result userList = this.userService.selectList(index,size);
        return userList;
    }

    @GetMapping("/select")
    @ApiOperation("模糊查询")
    public Result select(Integer index,Integer size,User user,@RequestHeader("Authorization") String token){
        Result userList = this.userService.select(index,size,user);
        return userList;
    }

    @GetMapping("/currentUser")
    @ApiOperation("通过token获取用户信息")
    public Result currentUser(@RequestHeader("Authorization") String token){
        return this.userService.selectCurrentUser(token);
    }


}
